import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:get/get.dart';
import 'package:kq_flutter_widgets/pages/common/file_preview/state.dart';
import 'package:kq_flutter_widgets/resources/l10n/kq_intl.dart';
import 'package:kq_flutter_widgets/widgets/emptyView/empty_view.dart';
import 'package:kq_flutter_widgets/widgets/titleBar/kq_title_bar.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../../../resources/kq_theme_colors.dart';
import '../../../widgets/progressbar/kq_horizontal_progress_bar.dart';
import 'controller.dart';

/// 文件预览
///
/// 注意：仅支持在线文件预览，不要传本地文件路径
class FilePreviewPage extends StatelessWidget {
  const FilePreviewPage({super.key});

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(FilePreviewController());
    final state = Get.find<FilePreviewController>().state;

    return Scaffold(
      backgroundColor: KqThemeColors.bgWhite,
      appBar: KqHeadBar(
        headTitle:
            Get.arguments['customTitle'] ?? Get.arguments['fileName'] ?? '',
      ),
      body: GetBuilder<FilePreviewController>(
          init: controller,
          builder: (controller) {
            return Column(
              children: [
                //顶部进度条视图
                Visibility(
                    visible: state.showProgress,
                    child: KqHorizontalProgressBar(
                      progress: state.progress,
                      pbColor: KqThemeColors.bgBlue,
                      height: 4.r,
                    )),
                //文件加载失败视图
                Visibility(
                    visible: state.status == FilePreviewStatus.failed,
                    child: Expanded(
                      child: KqEmptyView(
                        text: KqIntl.currentResource.fileLoadFail,
                      ),
                    )),
                //文件不支持类型视图
                Visibility(
                    visible: state.status == FilePreviewStatus.notSupport,
                    child: Expanded(
                      child: KqEmptyView(
                        text: KqIntl.currentResource.notSupportPreview,
                      ),
                    )),
                //加载中视图
                Visibility(
                    visible: state.status == FilePreviewStatus.loading,
                    child: Expanded(
                      child: Container(
                        alignment: Alignment.center,
                        child: SizedBox(
                          width: 80.r,
                          height: 80.r,
                          child: const CircularProgressIndicator(),
                        ),
                      ),
                    )),
                //txt视图
                Visibility(
                    visible: !state.useWebView &&
                        state.status == FilePreviewStatus.success,
                    child: Expanded(
                      child: SingleChildScrollView(
                          child: Padding(
                        padding: EdgeInsets.all(10.r),
                        child: Text(
                          state.txtContent,
                          style: TextStyle(
                              fontSize: 16.sp, color: KqThemeColors.text26),
                        ),
                      )),
                    )),
                //浏览器视图
                Visibility(
                  visible: state.useWebView &&
                      state.status == FilePreviewStatus.success,
                  child: Expanded(
                      child: WebViewWidget(
                    controller: controller.webController,
                  )),
                )
              ],
            );
          }),
    );
  }
}
